package com.dy.scs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dy.scs.pojo.Result;
import com.dy.scs.pojo.Student;
import com.dy.scs.service.StudentService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 学生
 */
@RestController
@Slf4j
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;

    /**
     * 查询
     * @param sno
     * @returnscs
     */
    @GetMapping("/{sno}")
    public Result<Student> getBySno(@RequestParam(required = false) String sno,
                                    @RequestParam(required = false) String sname,
                                    @RequestParam(required = false) String ssex,
                                    @RequestParam(required = false) Integer sage,
                                    @RequestParam(required = false) String sdept){

        Student student = studentService.getById(sno);
        return Result.success(student);
    }

    /**
     *更新
     */
    @PutMapping("/update")
    public Result update(@RequestBody Student student){
        studentService.updateById(student);
        return Result.success();
    }

    /**
     *删除
     * @param sno
     * @return
     */
    @DeleteMapping("/delete")
    public Result delete(@RequestParam List<String> sno){

        boolean b = studentService.removeBatchByIds(sno);
        if(!b)
        {
            return Result.error("删除失败");
        }
        return Result.success();
    }

    /**
     * 分页查询+模糊+排序
     */
    @GetMapping("/page")
    public Result pages(@RequestParam(defaultValue = "1") Integer pages,
                        @RequestParam(defaultValue = "10") Integer pageSize,
                        @RequestParam(required = false)String key,
                        @RequestParam(required = false)String sortBy,
                        @RequestParam(required = false)String sortType){
        Page<Student> page= studentService.pages(pages,pageSize,key,sortBy,sortType);
        return Result.success(page);
    }

    /**
     * 添加学生
     */
    @PostMapping("/insert")
    public Result inserts(@RequestBody Student student){
        boolean save = studentService.save(student);
        if(!save){
            return Result.error("添加失败");
        }
        return Result.success();
    }
}
